const doHandleMonth = (month) => {
  let m = month;
  if (month.toString().length == 1) {
    m = '0' + month;
  }
  return m;
};

export const obtainDay = (day) => {
  const today = new Date();
  const targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds); //注意，这行是关键代码
  const tYear = today.getFullYear();
  const tMonth = today.getMonth();
  const tDate = today.getDate();
  return new Date(tYear, tMonth, tDate);
};

export const getNowTime = () => {
  const date = new Date();
  const sign2 = ':';
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minutes = date.getMinutes();
  const seconds = date.getSeconds();

  return new Date(year, month, day, hour, minutes, seconds);
};

export const getNowTime2 = () => {
  const date = new Date();
  const sign2 = ':';
  const year = date.getFullYear();
  const month = date.getMonth();
  const day = date.getDate();
  const hour = date.getHours();
  const minutes = date.getMinutes();

  return new Date(year, month, day, hour, minutes);
};

export const getNowDate = () => {
  const date = new Date();
  const sign2 = ':';
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minutes = date.getMinutes();
  const seconds = date.getSeconds();
  const weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
  const week = weekArr[date.getDay()];
  return year + '-' + month + '-' + day + ' ' + hour + sign2 + minutes + sign2 + seconds;
};

/**
 * 截取字符串，
 * @param time （格式:yyyyMMddHHmmssfff）
 */
export const sliceTime = (time) => {
  // console.log(time);
  if (time) {
    const year = time.slice(0, 4);
    const month = time.slice(4, 6);
    const day = time.slice(6, 8);
    const sign2 = ':';
    const hour = time.slice(8, 10);
    const minutes = time.slice(10, 12);
    const seconds = time.slice(12, 14);
    return year + '-' + month + '-' + day + ' ' + hour + sign2 + minutes + sign2 + seconds;
  } else {
    return '';
  }
};

/**
 * 将时间格式转成  yyyyMMddHHmmssfff
 * @param time
 * @returns
 */
export const initFormat_one = (time) => {
  if (time) {
    const year = time.getFullYear;
    const month = time.getMonth + 1 < 10 ? '0' + (time.getMonth + 1) : time.getMonth + 1;
    const day = time.getDate();
    const hour = time.getHours();
    const minutes = time.getMinutes();
    const seconds = time.getSeconds();
    return year + month + day + hour + minutes + seconds + '000';
  } else {
    return '';
  }
};


/**
 * 将标准的时间格式修改为 "yyyy-mm-dd"
 * @param date 
 * @returns 
 */
export const formatDate = (date: Date) => {
  var year = date.getFullYear();
  var month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的
  var day = (date.getDate()).toString().padStart(2, '0');
  return year + '-' + month + '-' + day;
}

